package cn.tedu.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.dao.persist.repositoy.IUserDetailsRepository;
import cn.tedu.pojo.entity.UserDetails;
import cn.tedu.pojo.param.UserDetailsParam;
import cn.tedu.pojo.vo.UserDetailsVO;
import cn.tedu.service.IUserDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements IUserDetailsService {
    @Autowired
    private IUserDetailsRepository userDetailsRepository;

    @Override
    public int insert(CurrentPrincipal currentPrincipal, UserDetailsParam userDetailsParam) {
        UserDetails userDetails = new UserDetails();
        BeanUtils.copyProperties(userDetailsParam, userDetails);
        userDetails.setId(currentPrincipal.getId());
        userDetails.setUsername(currentPrincipal.getUsername());
        // TODO 根据用户id查询用户表 得到用户名，用户头像，角色名称，手机号和邮箱 给userDetails赋值
        return userDetailsRepository.insert(userDetails);
    }

    @Override
    public UserDetailsVO getStandard(Long userId) {
        return userDetailsRepository.getStandard(userId);
    }

    @Override
    public void updateInfo(Long userId, UserDetailsParam userDetailsParam) {
        UserDetails userDetails = new UserDetails();
        BeanUtils.copyProperties(userDetailsParam,userDetails);
        userDetails.setUserId(userId);
        int rows = userDetailsRepository.updateById(userDetails);
        if (rows != 1){
            String message = "修改基本信息失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updateUserUrl(Long userId, String userUrl) {
        UserDetails userDetails = new UserDetails();
        userDetails.setUserId(userId);
        userDetails.setUserUrl(userUrl);
        int rows = userDetailsRepository.updateById(userDetails);
        if (rows != 1){
            String message = "修改头像失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updatePhoneNumber(Long userId, String phoneNumber) {
        int count = userDetailsRepository.countByPhoneAndNotId(phoneNumber, userId);
        if (count > 0){
            String message = "修改手机号码失败，手机号码已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        UserDetails userDetails = new UserDetails();
        userDetails.setUserId(userId);
        userDetails.setPhoneNumber(phoneNumber);
        int rows = userDetailsRepository.updateById(userDetails);
        if (rows != 1){
            String message = "修改手机号码失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updateEmail(Long userId, String email) {
        int count = userDetailsRepository.countByEmailAndNotId(email, userId);
        if (count > 0) {
            String message = "修改电子邮箱失败，电子邮箱已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        UserDetails userDetails = new UserDetails();
        userDetails.setId(userId);
        userDetails.setEmail(email);
        int rows = userDetailsRepository.updateById(userDetails);
        if (rows != 1) {
            String message = "修改电子邮箱失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
